﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DBSourceToolsLib.CodeParser
{
    public class ResolvedWordList : IResolved
    {
        List<IResolved> m_children = new List<IResolved>();
        List<ParseObject> m_parseObjectList = new List<ParseObject>();

        public ResolvedWordList(List<ParseObject> listParseData)
        {
            int iBraceType = -1;
            iBraceType = ParseObjectList.FindBraceType(listParseData);
            if (iBraceType > -1)
            {
                int iFirstIdx, iLastIdx;
                List<ParseObject> matchedBraces = new List<ParseObject>();

                ParseObjectList.MatchStartAndEndBrace(listParseData, ref matchedBraces, out iFirstIdx, out iLastIdx, iBraceType);
                if ((iFirstIdx > -1) && (iLastIdx > -1))
                {
                    List<ParseObject> innerList = ParseObjectList.InnerList(matchedBraces, ParseObject.BRACE);
                    if (ParseObjectList.FindBraceType(innerList) > -1)
                    {
                        this.add(new ResolvedWordList(innerList));
                    }
                    for (int i = 0; i < iFirstIdx; i++)
                        m_parseObjectList.Add(listParseData[i]);
                    for (int i = iLastIdx +1; i < listParseData.Count; i++)
                        m_parseObjectList.Add(listParseData[i]);
                }
                else
                {
                    m_parseObjectList = listParseData;
                }

            }
            else
            {
                m_parseObjectList = listParseData;
            }

        }


        public ResolvedType getResolvedType()
        {
            return ResolvedType.ResolvedWordList;
        }

        public bool isLeaf()
        {
            return m_children.Count == 0;
        }

        public void add(IResolved iResolved)
        {
            m_children.Add(iResolved);
        }

        public System.Collections.IEnumerator getSubordinates()
        {
            return m_children.GetEnumerator();
        }

        public IResolved getChild()
        {
            throw new NotImplementedException();
        }

        public string DumpToString()
        {
            StringBuilder oBuilder = new StringBuilder();
            oBuilder.AppendLine(" Resolved Word List : ");
            

            foreach (ParseObject oParseObject in m_parseObjectList)
                oBuilder.AppendLine(oParseObject.DumpToString());

            oBuilder.AppendLine(" children : ");
            foreach (IResolved iChild in m_children)
                oBuilder.AppendLine(iChild.DumpToString());
            return oBuilder.ToString();
        }
    }
}
